/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[SendingMailWithAttachments.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.mail;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;

import org.junit.Before;
import org.junit.Test;

import javamail.utils.MailAuthenticator;
import javamail.utils.MailConstants;
import javamail.utils.PasswordUtils;

/**
 * 发送带内嵌图片, 附件的邮件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class SendingMailWithAttachments {

	private static Properties properties = null;

	private static String emailSender = "javamail_sns@sina.com";
	private static String emailRecipient = "18505106250@163.com";

	@Before
	public void init() {
		properties = new Properties();
		properties.setProperty("mail.transport.protocol", MailConstants.PROTOCOL_SMTP);
		properties.setProperty("mail.smtp.host", MailConstants.SMTP_HOST_OF_SINA);
		properties.setProperty("mail.smtp.port", MailConstants.SMTP_PORT_OF_SINA);
		properties.setProperty("mail.smtp.auth", String.valueOf(MailConstants.IS_AUTHORIZATION_NEED));
		properties.setProperty("mail.debug", String.valueOf(MailConstants.IS_DEBUG_MOD_ENABLED));
	}

	/**
	 * 发送Html附带附件的邮件
	 * 
	 * @throws Exception
	 */
	public static void sendHtmlMailWithAttachment() throws Exception {
		// 创建Session实例
		Session session = Session.getInstance(properties,
				new MailAuthenticator(emailSender, PasswordUtils.readPassword(emailSender)));

		// 创建MimeMessage实例
		MimeMessage message = new MimeMessage(session);

		// 封装Message的属性
		message.setSubject("JavaMail - 带有附件的Html邮件");
		message.setSender(new InternetAddress(emailSender, "不落的太阳", "UTF-8"));
		// 设置收件人, 抄送, 密送
		message.setRecipients(RecipientType.TO,
				new Address[] { new InternetAddress(emailRecipient, "不落的太阳", "UTF-8"), });
		message.setRecipient(RecipientType.CC, new InternetAddress(emailSender, "不落的太阳", "UTF-8"));
		message.setRecipient(RecipientType.BCC, new InternetAddress(emailSender, "不落的太阳", "UTF-8"));
		// 设置优先级
		message.setHeader("X-Priority", "1");
		// 设置回执
		message.setHeader("Disposition-Notification-To", emailSender);
		// 设置回复人(收件人回复邮件时默认的对方收件人)
		message.setReplyTo(InternetAddress.parse("\"" + MimeUtility.encodeText("不落的太阳") + "\"<javamail_sns@sina.com>"));
		message.setSentDate(new Date());

		// 创建一个mixed类型的MimeMultipart实例, mixed类型用于组合文本, 图片, 附件等
		MimeMultipart mailContent = new MimeMultipart("mixed");

		// 附件
		MimeBodyPart attachment1 = new MimeBodyPart();
		DataSource dataSource = new FileDataSource(new File("src/javamail/attachments/1.jpg"));
		DataHandler dataHandler = new DataHandler(dataSource);
		attachment1.setFileName(MimeUtility.encodeText("附件1.jpg"));
		attachment1.setDataHandler(dataHandler);
		mailContent.addBodyPart(attachment1);

		MimeBodyPart attachment2 = new MimeBodyPart();
		dataSource = new FileDataSource(new File("src/javamail/attachments/1.txt"));
		dataHandler = new DataHandler(dataSource);
		attachment2.setFileName(MimeUtility.encodeText("附件2.txt"));
		attachment2.setDataHandler(dataHandler);
		mailContent.addBodyPart(attachment2);

		// 内容
		MimeBodyPart mailBody = new MimeBodyPart();

		// 邮件正文内容[html + image]
		MimeMultipart mailBodyMP = new MimeMultipart("related");
		MimeBodyPart imagePart = new MimeBodyPart();
		MimeBodyPart htmlPart = new MimeBodyPart();
		mailBodyMP.addBodyPart(imagePart);
		mailBodyMP.addBodyPart(htmlPart);

		// 图片
		dataSource = new FileDataSource(new File("src/javamail/attachments/1.jpg"));
		dataHandler = new DataHandler(dataSource);
		imagePart.setDataHandler(dataHandler);
		imagePart.setContentID("image1.jpg");

		// 邮件内容
		MimeMultipart htmlMP = new MimeMultipart("alternative");
		MimeBodyPart htmlContent = new MimeBodyPart();
		htmlContent.setContent("<span style='color: red'>JavaMail - 带有附件的Html邮件 <img src='cid:image1.jpg' /></span>",
				"text/html;charset=UTF-8");
		htmlMP.addBodyPart(htmlContent);
		htmlPart.setContent(htmlMP);

		mailBody.setContent(mailBodyMP);
		mailContent.addBodyPart(mailBody);

		message.setContent(mailContent);

		Transport.send(message);
	}

	@Test
	public void testSendHtmlMailWithAttachment() throws Exception {
		sendHtmlMailWithAttachment();
	}
}
